import Mock from 'mockjs'

/**
 * 模拟所有数据
 */
const { list } = Mock.mock({
    'list|200': [
        {
            'id|+1': 1,
            'projectName|1': ['心脑血管', '高血压高血糖'],
            'timeNum|1-5': 2,
            'projectType|1':  ['付费项目', '免费项目'],
            'projectPrice|9-99': 3,
            projectTitle: '@ctitle(20, 50)',
        }
    ]
})

let _id = 201

export default [
  { // 分页查询表格数据
    url: '/api/service/projects',
    method: 'get',
    response: ({ query: { page = 1, pageSize = 10, projectType = '全部', keywords } }) => {
      // 根据条件筛选数据
      const filteredList = list.filter(item => {
        if (projectType === '全部') {
            return true
        }
        return item.projectType === projectType
      }).filter(item => {
        return keywords ? item.projectTitle.includes(keywords) || item.projectName.includes(keywords) : true
      })
      const total = filteredList.length // 数据量
      // 按照接口规范模拟数据
      const data = {
        list: filteredList.slice((page - 1) * pageSize, page * pageSize),
        total,
        page: Number(page),
        pageSize: Number(pageSize),
        isEnd: page * pageSize >= total
      }

      return {
        code: 200,
        data
      }
    },
  },
  { // 添加
    url: '/api/service/projects',
    method: 'post',
    response: ({body}) => {
      // 修改数据
      if(body.id) {
        // let goods = list.find(item => {
        //   return item.id === body.id
        // })
        let index1 = 0
        list.forEach((item, index) => {
          if(item.id === body.id){
            index1 = index
          }
        })

        // for(let k in goods){
        //   goods[k] = body[k]
        // }
        list.splice(index1, 1 ,{...body})
      }else {
        list.push({
            ...body,
            id: _id++,
          })
      }
      

      return {
        code: 200,
        data: {
            status: 200,
            message: '添加成功!',
        }
      }
    },
  },
//   { // 查询
//     url: '/api/service/projects',
//     method: 'post',
//     response: ({ value }) => {
//         const typeList = list.filter(item => item.projectType === value)
//         const total = typeList.length // 数据量
//         const data = {
//             list: typeList,
//             total,
            
//           }
//         return {
//             code: 200,
//             data
//         }
        
//     }
//   },
//   {//tianjia 
//     url: '/api/service/projects',
//     method: 'delete',
//     response: ()
//   }
{ // 删除
    url: '/api/service/projects',
    method: 'delete',
    response: ({query: { page = 1, pageSize = 10, id}}) => {
    //   删除数据
        let index1 = 0
        list.forEach((item, index) => {
          if(item.id === id) {
            index1 = index
          }
        })
        list.splice(index1,1)
        const total = list.length // 数据量
        const data = {
            list: list.slice((page - 1) * pageSize, page * pageSize),
            total,
            page: Number(page),
            pageSize: Number(pageSize),
            isEnd: page * pageSize >= total,
            message: '删除成功'
          }
        return {
            code: 200,
            data
      }
    },
  },
 
]
